package com.pzhyd.sc.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.pzhyd.sc.dao.inter.IBaseDao;
import com.pzhyd.sc.util.HibernateUtil;
import com.pzhyd.sc.util.Page;


public class BaseDaoImpl implements IBaseDao {


	public int delete(Object obj) throws Exception {
		Session ses = HibernateUtil.getThreadLocalSession();
		ses.beginTransaction();
		ses.delete(obj);
		ses.getTransaction().commit();
		HibernateUtil.closeThreadLocalSession();
		return 1;
	}


	public int deleteById(Class clz, Serializable id) throws Exception {
		Session ses = HibernateUtil.getThreadLocalSession();
		Object obj = ses.get(clz, id);
		ses.beginTransaction();
		ses.delete(obj);
		ses.getTransaction().commit();
		HibernateUtil.closeThreadLocalSession();
		return 1;
	}


	public int getTotalCount(String hql, List params) throws Exception {
		Session ses = HibernateUtil.getThreadLocalSession();
		Query query = ses.createQuery(hql);
		this.setParam(query, params);
		int count = Integer.parseInt(query.uniqueResult()+"");
		HibernateUtil.closeThreadLocalSession();
		return count;
	}

	
	public int insert(Object obj){
		Session ses = HibernateUtil.getThreadLocalSession();
		ses.beginTransaction();
		try {
			ses.save(obj);
			
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			ses.getTransaction().rollback();
		}
		finally{
			ses.getTransaction().commit();
			//ses.flush();
		}
		HibernateUtil.closeThreadLocalSession();
		return 1;
	}

	public int operateHql(String hql, List params) throws Exception {
		Session ses = HibernateUtil.getThreadLocalSession();
		ses.beginTransaction();
		Query query = ses.createQuery(hql);
		this.setParam(query, params);
		int count = query.executeUpdate();
		ses.getTransaction().commit();
		HibernateUtil.closeThreadLocalSession();
		return count;
	}


	public Object searchById(Class clz, Serializable id) throws Exception {
		Session ses = HibernateUtil.getThreadLocalSession();
		Object obj = ses.get(clz, id);
		HibernateUtil.closeThreadLocalSession();
		return obj;
	}


	public void searchHql(String hql, List params, Page page) throws Exception {
		Session ses = HibernateUtil.getThreadLocalSession();
		Query query = ses.createQuery(hql);
		System.out.println("当前页"+page.getPageNo());
		query.setFirstResult((page.getPageNo()-1)*page.getPageSize());
		query.setMaxResults(page.getPageSize());
		this.setParam(query, params);
//		System.out.println("得到数据"+query.list());
		if(query.list().equals("[]")){
			page.setData(null);
		}
		else{
		page.setData(query.list());
		}
		HibernateUtil.closeThreadLocalSession();
	}
	
	public List searchHql(String hql, List params) throws Exception {
//		System.out.println("这里开始执行..");
		Session ses = HibernateUtil.getThreadLocalSession();
		Query query = ses.createQuery(hql);
		this.setParam(query, params);
		List lst =null;
		System.out.println("在dao中的lst"+query.list()+"  "+query.list().size());
		if(query.list().size()==0){
		}
		else{
			lst=query.list();
		}
		HibernateUtil.closeThreadLocalSession();
		return lst;
	}


	public int update(Object obj) throws Exception {
		Session ses = HibernateUtil.getThreadLocalSession();
		ses.beginTransaction();
		ses.update(obj);
		ses.getTransaction().commit();
		HibernateUtil.closeThreadLocalSession();
		return 1;
	}
	
	private void setParam(Query q, List params){
		if (params != null && params.size() > 0) {
			for (int i = 0; i < params.size(); i++) {
				q.setParameter(i, params.get(i));
			}
		}
	}



}
